package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.IndexDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = IndexService.class)
@Transactional
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexDao indexDao;

    @Override
    public void add(Index index) {
        indexDao.add(index);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
        Page<Index> pages = indexDao.findByCondition(queryString);

        return new PageResult(pages.getTotal(), pages.getResult());

    }

    @Override
    public Index findById(Integer id) {
        return indexDao.findById(id);
    }

    @Override
    public void edit(Index index) {
        indexDao.edit(index);
    }

    @Override
    public List<Index> findAll() {
        return indexDao.findAll();
    }
}
